package schema

import (
	"lianzi_gos/internal/config"
	"time"

	"lianzi_gos/pkg/util"
)

// 我要买
type Procure struct {
	ID          string    `json:"id" gorm:"size:20;primarykey;"`  // Unique ID
	UserName    string    `json:"user_name" gorm:"size:32;index"` // 购买名称
	Name        string    `json:"name" gorm:"size:32;index"`      // 购买名称
	Img         string    `json:"img" gorm:"size:1024;index"`     // 购买图片
	Phone       string    `json:"phone" gorm:"size:20;index"`     // 购买图片
	UID         string    `json:"uid" gorm:"size:20;index"`       // 发布人
	WID         string    `json:"w_id" gorm:"size:20;index"`      // 跟踪人
	Description string    `json:"description" gorm:"size:1024"`   // Details about dictionary
	Sequence    int       `json:"sequence" gorm:"index;"`         // Sequence for sorting
	Types       int       `json:"types" gorm:"index;"`            // 0 删除 1发布  2已联系
	Status      string    `json:"status" gorm:"size:20;index"`    // Status of dictionary (disabled, enabled)
	CreatedAt   time.Time `json:"created_at" gorm:"index;"`       // Create time
	SendAt      time.Time `json:"send_at" gorm:"index;"`          // 分配时间
	OkAt        time.Time `json:"ok_at" gorm:"index;"`            // 联系时间
	UpdatedAt   time.Time `json:"updated_at" gorm:"index;"`       // Update time
}

func (a *Procure) TableName() string {
	return config.C.FormatTableName("procure")
}

type ProcureAdmin struct {
	ID          string    `json:"id"`
	Name        string    `json:"name"`
	UserName    string    `json:"user_name"`
	Phone       string    `json:"phone"`
	Img         string    `json:"img"`
	StaffName   string    `json:"staff_name"`
	StaffPhone  string    `json:"staff_phone"`
	Types       int       `json:"types"`
	Description string    `json:"description"`
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
	OkAt        time.Time `json:"ok_at"`
}

// Defining the query parameters for the `Procure` struct.
type ProcureQueryParam struct {
	util.PaginationParam

	LikeName     string `form:"name"`      // 购买名称
	LikeUserName string `form:"user_name"` // 购买人
	LikePhone    string `form:"phone"`     // 购买电话
	Source       int64  `form:"source"`    // 来源
	Types        int64  `form:"types"`     // 来源
	WID          string `form:"-"`         // 购买名称
	Status       string `form:"-"`         // Status of dictionary (disabled, enabled)
}

// Defining the query options for the `Procure` struct.
type ProcureQueryOptions struct {
	util.QueryOptions
}

// Defining the query result for the `Procure` struct.
type ProcureQueryResult struct {
	Data       Procures
	PageResult *util.PaginationResult
}
type ProcureQueryAdminResult struct {
	Data       ProcureAdmins
	PageResult *util.PaginationResult
}

// Defining the slice of `Procure` struct.
type Procures []*Procure
type ProcureAdmins []*ProcureAdmin

func (a Procures) Len() int {
	return len(a)
}

// Defining the data structure for creating a `Procure` struct.
type ProcureForm struct {
	Name        string `json:"name" binding:"required,max=32"`      // 购买名称
	UserName    string `json:"user_name" binding:"required,max=32"` // 购买名称
	Phone       string `json:"phone" binding:"required,max=32"`     // 购买名称
	Img         string `json:"img"`                                 // 图片
	Description string `json:"description"`                         // Details about dictionary
}
type ProcureContactForm struct {
	WID   string `json:"w_id"`  // 购买名称
	Types string `json:"types"` // 购买名称
}

// A validation function for the `ProcureForm` struct.
func (a *ProcureForm) Validate() error {
	return nil
}

// Convert `ProcureForm` to `Procure` object.
func (a *ProcureForm) FillTo(procure *Procure) error {
	procure.Name = a.Name
	procure.UserName = a.UserName
	procure.Phone = a.Phone
	procure.Img = a.Img
	procure.Description = a.Description
	procure.Status = "enabled"
	procure.Types = 1
	return nil
}
func (a *ProcureContactForm) FillToContact(procure *Procure) error {
	procure.WID = a.WID
	return nil
}

// 标记已联系
func (a *ProcureContactForm) FillToContactType(procure *Procure) error {
	procure.Types = 2
	return nil
}
